package com.example.myhelloworld.server;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.example.myhelloworld.client.TextProblem;
import com.google.appengine.api.datastore.Key;

import com.example.myhelloworld.client.TPInterface;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.util.Vector;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TextProblemSet extends RemoteServiceServlet implements TPInterface {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;
	
  @Persistent
  Vector<String> texts = new Vector<String>();;
  @Persistent
  Vector<String> formulas = new Vector<String>();;
  @Persistent
  Vector<String> varbounds = new Vector<String>();;
  @Persistent
  Vector<String> answers = new Vector<String>();;
  @Persistent
  Vector<String> comments = new Vector<String>();;

  /**
   *The constructor can contain some predefined problems.
   */
  public TextProblemSet() {
    if (texts.isEmpty()) {
	  texts.add("How much wood would x1 woodchucks chuck in x2 minutes, \nif x1 woodchucks would chuck x3 units of wood per minute?");
	  formulas.add("x2 * x3");
	  varbounds.add("2 10 2 10 2 10");
	  answers.add("");
	  comments.add("Use multiplication.");
	  
	  texts.add("If your x1 friends each have x2 books. How many books are there?");
	  formulas.add("x1 * x2");
	  varbounds.add("1 6 2 7");
	  answers.add("");
	  comments.add("Just multiply the numbers.");

	}
  }
  
  /**
  *Add a new problem involving random numbers.
  */
  public void addProblem(String text, String formula, String varbound, String comment) {
    texts.add(text);
    formulas.add(formula);
    varbounds.add(varbound);
    answers.add("");
    comments.add(comment);
  }

  /**
  *Add a new static text problem.
  */
  public void addProblem(String text, String answer, String comment) {
    texts.add(text);
    formulas.add("");
    varbounds.add("");
    answers.add(answer);
    comments.add(comment);	
  }

  /**
   *Returns a specific problem as a "tuple", a vector, (text, formula, varbounds, answer, comment).
   *If called with too high index, it will return the most recent problem added.
   */
  public Vector<String> getProblem(int i) {
    if (i>=texts.size()) {
	  System.out.println("Warning: Getting a text problem with too high index!");
	  i = texts.size()-1;
	}
	Vector<String> v = new Vector<String>();
	v.add(texts.get(i));
	v.add(formulas.get(i));
	v.add(varbounds.get(i));
	v.add(answers.get(i));
	v.add(comments.get(i));
    return v;
  }
  
  public Vector<String> getRandomProblem() {
    int i = (new java.util.Random()).nextInt(texts.size());
	Vector<String> v = new Vector<String>();
	v.add(texts.get(i));
	v.add(formulas.get(i));
	v.add(varbounds.get(i));
	v.add(answers.get(i));
	v.add(comments.get(i));
    return v;
  }

  
  /**
  *Get the number of problems by querying the texts vector. All others should be the same size, if not, we're doomed.
  */
  public int getNumberOfProblems() {
    return texts.size();
  }
  
  public Key getKey() {
    return key;
  }

  //Main test routine:
  public static void main(String[] args) {
    TextProblemSet set = new TextProblemSet();

    //Add a couple of problems to the database
    set.addProblem("What is x1 + x2?", "x1 + x2", "1 20 1 20", "Simply add x1 and x2 together!");
    set.addProblem("Who are you?", "I am me", "This is a deep philosophical question!");
//
//    TextProblem tp = set.getRandomProblem();
//    System.out.println(tp.getProblemText());
//    System.out.println(tp.getFormula());
//    System.out.println(tp.getAnswer());
//    System.out.println(tp.getComment());

  }
}